package com.ysd.product.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.ysd.common.entity.Img;
import com.ysd.common.entity.Product;
import com.ysd.common.entity.ProductEChart;
import com.ysd.product.service.ProductService;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

@Controller
@RequestMapping("/products")
public class ProductController {
	@Autowired
	private ProductService productService;
	
	
	@Value("${server.port}")
	private String port;
	
	@Value("${spring.cloud.client.ip-address}")
	private String ip;
	
	/**
	 * 测试路径:http://localhost:9002/products/findAllBySelAndPage?name=耐克&f_pcid=2&isxj=0&isxss=1&isxsg=1&page=1&limit=10
	 * 根据条件查询并分页
	 * @param product
	 * @return
	 */
	@RequestMapping("/findAllBySelAndPage")
	@ResponseBody
	public Map<String, Object> findAllBySelAndPage(Product product,int page,int limit){
		System.out.println("product="+product); 
		page=(page-1)*10;
		List<Product> list=productService.findAllBySelAndPage(product,page,limit);
		int total=productService.findProductTotal(product, page, limit);
		Map<String, Object> map=new HashMap<String, Object>();
		map.put("data", list);
		map.put("code", 0);
		map.put("count", total);
		return map;
	}
	/**
	 * 测试路径:http://localhost:9002/products/saveproduct?name=阿迪达斯&fPcid=4&oldprice=299&newprice=199&plate=阿迪达斯&size=XXXL&cate=韩版&factory=江苏服装厂&area=江苏&description=宽松&imgzt=aaa&isxj=0
	 * 保存
	 * @param product
	 * @return
	 */
	@RequestMapping("/saveproduct")
	@ResponseBody
	public Map<String, Object> saveProduct(Product product) {
		int r=productService.saveProduct(product);
		Map<String, Object> map=new HashMap<String, Object>();
		if(r>0) {
			map.put("code", 0);
			map.put("success", true);
			map.put("message", "添加成功");
		}else {
			map.put("code", 0);
			map.put("success", false);
			map.put("message", "添加失败");
		}
		return map;
	}
	/**
	 * 测试路径:http://localhost:9002/products/updateproduct?name=阿迪达斯&fPcid=4&oldprice=299&newprice=199&plate=阿迪达斯&size=XL&cate=韩版&factory=江苏服装厂&area=江苏&description=宽松&imgzt=aaa&isxj=0&id=70
	 * 修改
	 * @param product
	 * @return
	 */
	@RequestMapping("/updateproduct")
	@ResponseBody
	public Map<String, Object> findById(Product product){
		Map<String, Object> map=new HashMap<String, Object>();
		int r=productService.updateProduct(product);
		if(r>0) {
			map.put("success", true);
			map.put("message", "修改成功");
			map.put("code", 0);
		}else {
			map.put("success", false);
			map.put("message", "修改失败");
			map.put("code", 0);
		}
		return map;
	}
	/**
	 * 删除
	 * 测试路径:http://localhost:9002/products/delProduct?id=68
	 * @param id
	 * @return
	 */
	@RequestMapping("/delProduct")
	@ResponseBody
	public Map<String, Object> delProduct(int id){
		int r=productService.delProduct(id);
		Map<String, Object> map=new HashMap<String, Object>();
		if(r>0) {
			map.put("success", true);
			map.put("message", "删除成功");
			map.put("code", 0);
		}else {
			map.put("success", false);
			map.put("message", "删除失败,此商品不能删除");
			map.put("code", 0);
		}
		return map;
	}
	/**
	 * 修改商品是否推荐到首页
	 * 测试路径:http://localhost:9002/products/updateProductIsXss?isxss=1&id=70
	 * @param isxss
	 * @param id
	 * @return
	 */
	@RequestMapping("/updateProductIsXss")
	@ResponseBody
	public Map<String, Object> updateProductIsXss(int isxss,int id){
		int r=productService.updateProductIsXss(isxss, id);
		Map<String, Object> map=new HashMap<String, Object>();
		if(r>0) {
			map.put("success", true);
			map.put("message", "修改成功");
			map.put("code", 0);
		}else {
			map.put("success", false);
			map.put("message", "修改失败");
			map.put("code", 0);
		}
		return map;
	}
	/**
	 * 修改商品是否首页轮播图
	 * 测试路径:http://localhost:9002/products/updateProductIsXsg?isxsg=1&id=70
	 * @param isxsg
	 * @param id
	 * @return
	 */
	@RequestMapping("/updateProductIsXsg")
	@ResponseBody
	public Map<String, Object> updateProductIsXsg(int isxsg,int id){
		int r=productService.updateProductIsXsg(isxsg, id);
		Map<String, Object> map=new HashMap<String, Object>();
		if(r>0) {
			map.put("success", true);
			map.put("message", "修改成功");
			map.put("code", 0);
		}else {
			map.put("success", false);
			map.put("message", "修改失败");
			map.put("code", 0);
		}
		return map;
	}
	/**
	 * 商品上架、下架
	 * 测试路径:http://localhost:9002/products/updateProductIsXj?isxj=1&id=70
	 * @param isxj
	 * @param id
	 * @return
	 */
	@RequestMapping("/updateProductIsXj")
	@ResponseBody
	public Map<String, Object> updateProductIsXj(int isxj,int id){
		int r=productService.updateProductIsXj(isxj, id);
		Map<String, Object> map=new HashMap<String, Object>();
		if(r>0) {
			map.put("success", true);
			map.put("message", "修改成功");
			map.put("code", 0);
		}else {
			map.put("success", false);
			map.put("message", "修改失败");
			map.put("code", 0);
		}
		return map;
	}
	/**
	 * 购物车中检索商品是否有效
	 * 测试路径:http://localhost:9002/products/checkProduct?str=[{id:1,newprice:169,ext1:2},{id:2,newprice:149,ext1:2}]
	 * @param str
	 * @return
	 */
	@RequestMapping("/checkProduct")
	@ResponseBody
	public Map<String, Object> checkProduct(String str){
		return (Map<String, Object>) productService.findProductById(str);
	}
	/**
	 * 前台推送上架商品，按照编号降序取前5条
	 * 测试路径http://localhost:9002/products/findAllProByIndex
	 * @return
	 */
	@RequestMapping("/findAllProByIndex")
	@ResponseBody
	public List<Product> findAllProByIndex(){
		List<Product> list=productService.findAllProByIndex();
		return list;
	}
	/**
	 * 前台推送热门商品，按照权重降序取前5条
	 * 测试路径http://localhost:9002/products/findProBySellNum
	 * @return
	 */
	@RequestMapping("/findProBySellNum")
	@ResponseBody
	public List<Product> findProBySellNum(){
		List<Product> list=productService.findProBySellNum();
		return list;
	}
	/**
	 * 前台商品图片轮播
	 * 测试路径http://localhost:9002/products/findAllProByIsxsg
	 * @return
	 */
	@RequestMapping("/findAllProByIsxsg")
	@ResponseBody
	public List<Product> findAllProByIsxsg(){
		List<Product> list=productService.findAllProByIsxsg();
		return list;
	}
	/**
	 * 前台商品页面多条件分页商品展示
	 * 测试路径http://localhost:9002/products/findAllProByProlist?f_pcid=8&key=p.id desc&page=1&limit=9
	 * @param key
	 * @param page
	 * @param limit
	 * @return map
	 */
	@RequestMapping("/findAllProByProlist")
	@ResponseBody
	public Map<String, Object> findAllProByProlist(int f_pcid,String key,int page, int limit){
		Map<String, Object> map=new HashMap<String, Object>();
		page=(page-1)*9;
		List<Product> list=productService.findAllProByProlist(f_pcid,key,page, limit);
		int count=productService.findAllProByProlistTotal(f_pcid,key, page, limit);
		Product p=new Product();
		if(f_pcid>0) {
			p=productService.findProCateNameById(f_pcid);
			map.put("pcname", p.getPcname());
		}
		map.put("data", list);
		map.put("count", count);
		return map;
	}
	/**
	 * 商品类型——所属类型商品数量统计
	 * 测试路径http://localhost:9002/products/findProCateEChart
	 * @return map
	 */
	@RequestMapping("/findProCateEChart")
	@ResponseBody
	public Map<String, Object> findProCateEChart(){
		Map<String, Object> map=new HashMap<String, Object>();
		List<ProductEChart> list=productService.findProCateEChart();
		List<String> xAxis=new ArrayList();
		List<Integer> series=new ArrayList();
		for(int i=0;i<list.size();i++) {
			xAxis.add(list.get(i).getPcname());
			series.add(list.get(i).getNum());
		}
		map.put("xAxis", xAxis);
		map.put("series", series);
		return map;
	}
	/**
	 * 前台商品详情
	 * 测试路径http://localhost:9002/products/findProById?id=1
	 * @param id
	 * @return
	 */
	@RequestMapping("/findProById")
	@ResponseBody
	public Product findProById(int id){
		return productService.findProById(id);
	}
	/**
	 * 查询商品图片
	 * 测试路径http://localhost:9002/products/findAllImgByPId
	 * @param id
	 * @return
	 */
	@RequestMapping("/findAllImgByPId")
	@ResponseBody
	public Map<String, Object> findAllImgByPId(int id){
		Map<String, Object> map=new HashMap<String, Object>();
		List<Img> list=productService.findAllImgByPId(id);
		map.put("data", list);
		map.put("code", 0);
		return map;
	}
	/**
	 * 商品图片批量添加
	 * 测试路径http://localhost:9002/products/addProImgs
	 * @param imgs
	 * @return
	 */
	@RequestMapping("/addProImgs")
	@ResponseBody
	public Map<String, Object> addProImgs(Img imgs){
		Map<String, Object> map=new HashMap<String, Object>();
		String str=imgs.getImg().substring(0, imgs.getImg().length()-1);
		String arr[]=str.split(",");
		Img img=new Img();
		int r=0;
		for(int i=0;i<arr.length;i++) {
			img.setFPid(imgs.getFPid());
			img.setImg(arr[i]);
			r=productService.addProImgs(img);
		}
		if(r>0) {
			map.put("success", true);
			map.put("message", "添加成功");
			map.put("code", 0);
		}else {
			map.put("success", false);
			map.put("message", "添加失败");
			map.put("code", 0);
		}
		return map;
	}
	/**
	 *商品图片删除
	 *测试路径http://localhost:9002/products/delProImg 
	 * @param id
	 * @return
	 */
	@RequestMapping("/delProImg")
	@ResponseBody
	public Map<String, Object> delProImg(int id){
		Map<String, Object> map=new HashMap<String, Object>();
		int r=productService.delProImg(id);
		if(r>0) {
			map.put("success", true);
			map.put("message", "删除成功");
			map.put("code", 0);
		}else {
			map.put("success", false);
			map.put("message", "删除失败");
			map.put("code", 0);
		}
		return map;
	}
	/**
	 * //商品畅销查询，按照销量降序取前5条
	 * 测试路径http://localhost:9002/products/findProBySNum
	 * @return
	 */
	@RequestMapping("/findProBySNum")
	@ResponseBody
	public List<Product> findProBySNum(){
		List<Product> list=productService.findProBySNum();
		return list;
	}
}
